
package com.base.commons.elasticsearch.service.impl;


import cn.hutool.core.util.StrUtil;
import com.base.commons.elasticsearch.dao.EsBaseDao;
import com.base.commons.elasticsearch.toolkit.ReflectionKit;
import com.base.commons.elasticsearch.config.EsIndexPrefixConfig;
import com.base.commons.elasticsearch.service.EsBaseService;
import com.base.commons.tools.page.PageData;
import com.base.commons.tools.utils.ConvertUtils;
import org.frameworkset.elasticsearch.ElasticSearchException;
import org.frameworkset.elasticsearch.entity.ESDatas;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.io.Serializable;
import java.util.*;


/**
 * 基础服务类，所有Service都要继承
 *
 * @author carl
 * @since 1.0.0
 */
public abstract class EsBaseServiceImpl<M extends EsBaseDao<T>, T>  implements EsBaseService<T>{
    /**
     * 获取当前激活环境
     */
    @Value("${spring.profiles.active:dev}")
    public  String activePrefix;

    @Autowired(required = false)
    private EsIndexPrefixConfig esIndexPrefixConfig;
    /**
     * 获取index名称
     * @return 返回index名称
     */
    public String getIndexName() {
        String indexPrefix = Optional.ofNullable(esIndexPrefixConfig)
                .map(data ->data.getIndexPrefix())
                .orElse(activePrefix);
        return indexPrefix + "_" + StrUtil.removeSuffix(ReflectionKit.translate(mapperClass.getSimpleName()), SUFFIX) ;
    }

    protected Class<T> currentModelClass() {
        return (Class<T>) ReflectionKit.getSuperClassGenericType(getClass(), 1);
    }
    protected Class<?> entityClass = currentModelClass();
    protected Class<?> mapperClass = currentMapperClass();
    protected Class<T> currentMapperClass() {
        return (Class<T>) ReflectionKit.getSuperClassGenericType(getClass(), 0);
    }

    @Autowired
    protected M baseDao;
    @Override
    public M getBaseMapper() {
        return baseDao;
    }
    protected <T> PageData<T> getPageData(List<?> list, long total, Class<T> target){
        List<T> targetList = ConvertUtils.sourceToTarget(list, target);
        return new PageData<>(targetList, total);
    }

    @Override
    public String insertBatch(List<T> entityList) {
        return baseDao.addDocuments(getIndexName(), ELASTICSEARCH_TYPE, entityList, "refresh=wait_for");
    }

    @Override
    public List<T> selectBatchIds(Collection<? extends Serializable> idList){
        return (List<T>) baseDao.mgetDocuments(getIndexName(), ELASTICSEARCH_TYPE, entityClass, idList.toArray());
    }
    @Override
    public long selectCount(Map<String, Object> paramMap){
        //demo为索引表，_search为检索操作action
        return baseDao.count(getIndexName(),
                COUNT,
                paramMap);
    };
    @Override
    public ESDatas<T> searchList(Map<String, Object> paramMap){
        //demo为索引表，_search为检索操作action
        return (ESDatas<T>) baseDao.searchList(getIndexName()+_SEARCH,
                SEARCH_PAGING_DATA,
                paramMap,
                entityClass);
    }
    @Override
  public ESDatas<T> searchListScroll(Map<String, Object> paramMap){
     return  (ESDatas<T>) baseDao.searchList(getIndexName()+_SEARCH+"?scroll=10m",
             SCROLL_DEFAULT_QUERY,
              paramMap,
              entityClass);
  }
  @Override
  public ESDatas<T> searchScroll(String scroll, String scrollId){
        return (ESDatas<T>) baseDao.searchScroll(scroll,scrollId,entityClass);
  }
    @Override
    public  String deleteScrolls(Set<String> scrollIds) throws ElasticSearchException{
        return baseDao.deleteScrolls(scrollIds);
    }
}